package pikes.peak;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Body;
import pikes.html.xhtml.Head;

/**
 * Html page class whose properties are request dependent, e.g: the real value is generated from the model or the http request.
 * 
 * @author Peter Bona
 *
 */
public class Page {

	public static final RequestProperty<CharSequence> EMPTY_TITLE = new RequestIndependentProperty<CharSequence>("");

	private RequestProperty<CharSequence> title = EMPTY_TITLE;

	private List<BlockComponent> components = new LinkedList<BlockComponent>();

	public RequestProperty<CharSequence> getTitle() {
		return title;
	}

	public static void main(String[] args) {
	    Page homePage = new Page();
	    homePage.setTitle(new RequestProperty<CharSequence>() {

		public CharSequence getValue(Map model,
			RequestContext requestContext) throws Exception {

		    return new Date().toString();
		}
		
	    });
	    System.out.println(new Date().toString());
	}
	
	public void setTitle(RequestProperty<CharSequence> title) {
		if (title == null) {
			this.title = EMPTY_TITLE;
		}
		else {
			this.title = title;
		}
	}

	public void setTitle(String title) {
		setTitle(new ApplicationContextMessageProvider(title));
	}

	public void add(BlockComponent component) {
		if (component != null) {
			components.add(component);
		}
	}

	public final void buildBody(Body body, Map model, RequestContext requestContext) throws Exception {
		for (BlockComponent component : components) {
			body.add(component.createHtml(model, requestContext));
		}
	}

	public void buildHeader(Head head, Map model, RequestContext requestContext) {
		
	}
}
